
<template>
  <!-- 1. 完成页面数据展示 -->
  <!-- 2. 页面动态样式 -->
  <!-- 3. 点击inpt框时候，获取check状态 -->
  <!-- 4. 点击li上删除按钮完成数据删除 -->
  <!-- 5. 头部添加数据 -->
  <!-- 6. 已完成和全部数据展示 -->
  <!-- 7. footer 全选按钮 -->
  <!-- 8. 全部已完成按钮功能 -->
  <!-- 9. 数据持久化保存 -->

  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <Header :todoList="todoList" :iptAdd="iptAdd" />
        <Content :todoList="todoList" :iptChange="iptChange" :liDel="liDel" />
        <Footer
          :todoList="todoList"
          :allChecked="allChecked"
          :clearAll="clearAll"
        />
      </div>
    </div>
  </div>
</template>

<script>
// import { nanoid } from "nanoid";
import Header from "./components/Header";
import Content from "./components/Content";
import Footer from "./components/Footer";
export default {
  name: "App",
  components: {
    Header,
    Content,
    Footer,
  },
  data() {
    return {
      // 数据存储，如果返还undefined，就存储[]
      todoList: JSON.parse(localStorage.getItem("todoList")) || [],
    };
  },
  computed: {},
  methods: {
    // 单个input框状态修改
    iptChange(id) {
      this.todoList.forEach((item) => {
        if (item.id === id) {
          item.done = !item.done;
        }
      });
    },
    // 单个数据删除
    liDel(index) {
      this.todoList.splice(index, 1);
      // this.todoList = this.todoList.filter((item)=>{
      //   if(item.id != id) return item
      // })
    },
    // 头部添加数据
    iptAdd(newTodo) {
      this.todoList.unshift(newTodo);
    },
    //全选和全不选
    allChecked(newCheck) {
      this.todoList.forEach((item) => {
        item.done = newCheck;
      });
    },
    // 清除全部已完成事件
    clearAll() {
      // 这个只是过滤了，没有把过滤后的 结果拿到
      this.todoList = this.todoList.filter((item) => !item.done);
    },
  },
  // 使用watch监听 数据发送
  watch: {
    immediate: true,
    deep: true,
    handler() {
      localStorage.getItem("todoList", JSON.stringify(this.todoList || []));
    },
  },
};
</script>
    

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>

